package cn.iocoder.ny.module.mall.enums;

import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import cn.iocoder.ny.module.mall.domain.MallProduct;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


public enum MallProductEnum {

    // FORMJSON判断
    FORM_JSON {
        @Override
        public void process(MallProduct mallProduct) {
            try {
                Object formJsonObj = mallProduct.getFormJson();
                String formJsonStr = formJsonObj != null ? formJsonObj.toString() : null;

                if (formJsonStr == null || formJsonStr.isEmpty()) {
                    mallProduct.setFormJson("{}");
                } else {
                    // 使用正则表达式替换 '=' 为 ':'，并将单引号替换为双引号
                    formJsonStr = formJsonStr.replace("=", ":")
                            .replace("'", "\"");

                    // 检查并修复 JSON 字符串格式
                    formJsonStr = correctJsonString(formJsonStr);

                    // 验证 JSON 格式
                    JSONObject formJson = new JSONObject(formJsonStr);
                    mallProduct.setFormJson(formJson.toString()); // 这里保持字符串格式
                }
            } catch (JSONException e) {
                throw new RuntimeException("Invalid JSON format in formJson: " + e.getMessage(), e);
            }
        }

        private String correctJsonString(String jsonStr) {
            // 移除不必要的空格并修正结构
            jsonStr = jsonStr.replaceAll("\\s*(,|:|\\{|\\})\\s*", "$1");
            // 添加缺失的引号
            jsonStr = jsonStr.replaceAll("([\\{,])([a-zA-Z0-9_]+):", "$1\"$2\":");
            return jsonStr;
        }
    },

    // IMAGEJSON判断
    IMAGES {
        @Override
        public void process(MallProduct mallProduct) {
            try {
                String imagesJsonStr = mallProduct.getImages() != null ? mallProduct.getImages().toString().trim() : null;

                if (imagesJsonStr == null || imagesJsonStr.isEmpty()) {
                    mallProduct.setImages("{}");
                } else {
                    // 使用 ObjectMapper 来验证和格式化 JSON
                    imagesJsonStr = formatJson(imagesJsonStr);
                    mallProduct.setImages(imagesJsonStr);
                }
            } catch (JsonProcessingException e) {
                throw new RuntimeException("Invalid JSON format in images: " + e.getMessage(), e);
            }
        }

        private String formatJson(String jsonStr) throws JsonProcessingException {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode jsonNode = objectMapper.readTree(jsonStr);
                return jsonNode.toString();
            } catch (JsonProcessingException e) {
                throw new JsonProcessingException("Invalid JSON format: " + e.getMessage()) {};
            }
        }
    };

    public abstract void process(MallProduct mallProduct);
    }
